﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TYBase;

namespace WPF_TY_UI
{
    public class PageEventArgs : RoutedEventArgs
    {
        public int PageIndex { get; set; }

        // 构造函数
        public PageEventArgs(RoutedEvent routedEvent, object source, int pageIndex)
            : base(routedEvent, source)
        {
            PageIndex = pageIndex;
        }
    }

    /// <summary>
    /// LSBPager.xaml 的交互逻辑
    /// </summary>
    public partial class LSBPager : UserControl
    {
        public LSBPager()
        {
            InitializeComponent();
            this.Loaded += LSBPager_Loaded;
        }

        private void LSBPager_Loaded(object sender, RoutedEventArgs e)
        {
            PART_CBOX_ListPageSize.SelectionChanged -= PART_CBOX_ListPageSize_SelectionChanged;
            PART_CBOX_ListPageSize.SelectionChanged += PART_CBOX_ListPageSize_SelectionChanged;
        }

        public static readonly RoutedEvent PageIndexChangedRoute = EventManager.RegisterRoutedEvent(
       "PageIndexChangedRoute",
       RoutingStrategy.Bubble,
       typeof(RoutedEventHandler),
       typeof(PageEventArgs)
                );

        public event RoutedEventHandler PageIndexChanged
        {
            add { AddHandler(PageIndexChangedRoute, value); }
            remove { RemoveHandler(PageIndexChangedRoute, value); }
        }

        public void FireEvent_PageIndexChanged(int pageIndex)
        {
            PageEventArgs args = new PageEventArgs(PageIndexChangedRoute, this, pageIndex);
            RaiseEvent(args);
        }



        #region 附加属性 DataCount
        public int DataCount { get { return (int)GetValue(DataCountProperty); } set { SetValue(DataCountProperty, value); } }
        public static readonly DependencyProperty DataCountProperty = DependencyProperty.Register("DataCount", typeof(int), typeof(LSBPager), new PropertyMetadata(default(int)));
        #endregion


        #region 附加属性 SelectedPageIndex
        public int SelectedPageIndex { get { return (int)GetValue(SelectedPageIndexProperty); } set { SetValue(SelectedPageIndexProperty, value); } }
        public static readonly DependencyProperty SelectedPageIndexProperty = DependencyProperty.Register("SelectedPageIndex", typeof(int), typeof(LSBPager), new PropertyMetadata(1));
        #endregion


        #region 附加属性 PageCount
        public int PageCount { get { return (int)GetValue(PageCountProperty); } set { SetValue(PageCountProperty, value); } }
        public static readonly DependencyProperty PageCountProperty = DependencyProperty.Register("PageCount", typeof(int), typeof(LSBPager), new PropertyMetadata(default(int)));
        #endregion


        #region 附加属性 PageIndex
        public int PageIndex { get { return (int)GetValue(PageIndexProperty); } set { SetValue(PageIndexProperty, value); } }
        public static readonly DependencyProperty PageIndexProperty = DependencyProperty.Register("PageIndex", typeof(int), typeof(LSBPager), new PropertyMetadata(1));
        #endregion


        #region 附加属性 PageSize
        public int PageSize { get { return (int)GetValue(PageSizeProperty); } set { SetValue(PageSizeProperty, value); } }
        public static readonly DependencyProperty PageSizeProperty = DependencyProperty.Register("PageSize", typeof(int), typeof(LSBPager), new PropertyMetadata(10, OnPageSizeChanged));

        private static void OnPageSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = (d as LSBPager);
            ctrl.PART_CBOX_ListPageSize.SelectionChanged -= ctrl.PART_CBOX_ListPageSize_SelectionChanged;
            ctrl.SelectedPageIndex = ctrl.listPageSize.IndexOf(ctrl.PageSize); 
            ctrl.PART_CBOX_ListPageSize.SelectionChanged += ctrl.PART_CBOX_ListPageSize_SelectionChanged;
        }
        #endregion

        private void BtnClick_PrePage(object sender, RoutedEventArgs e)
        {
            if (this.PageIndex > 1)
            {
                this.PageIndex--;
                FireEvent_PageIndexChanged(PageIndex);
            }
        }
        private void BtnClick_NextPage(object sender, RoutedEventArgs e)
        {
            if (this.PageIndex < PageCount)
            {
                this.PageIndex++;
                FireEvent_PageIndexChanged(PageIndex);
            }
        }

        private void LSBTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var currentPageIndex = txtCurrentPageIndex.Text.AsInt();
                if (currentPageIndex < PageCount && currentPageIndex > 0)
                {
                    this.PageIndex = currentPageIndex;
                    FireEvent_PageIndexChanged(PageIndex);
                }
            }
        }
        List<int> listPageSize = new List<int> { 5, 10, 20, 30, 40, 50 };
        private void PART_CBOX_ListPageSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (SelectedPageIndex < 0)
                return;
            if (SelectedPageIndex < listPageSize.Count)
            {
                this.PageSize = listPageSize[SelectedPageIndex];
                FireEvent_PageIndexChanged(PageIndex);
            }
        }
    }
}
